<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Laravel," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="自从Laravel 5.3开始，关于Eloquent ORM操作的返回值变成了Collection，不再是数组，意味着我们对Collection的操作将会变得越来越多，下面总结了一些Collection的常用方法，方便以后使用。
简介Illuminate\Support\Collection类提供了一个流畅、便利的封装来操控数组数据。
创建集合如上所述，collect() 辅助函数会利用传入的">
<meta property="og:type" content="article">
<meta property="og:title" content="Laravel Collection常用方法">
<meta property="og:url" content="http://yoursite.com/2017/06/05/Laravel-Collection常用方法/index.html">
<meta property="og:site_name" content="EasyBlog">
<meta property="og:description" content="自从Laravel 5.3开始，关于Eloquent ORM操作的返回值变成了Collection，不再是数组，意味着我们对Collection的操作将会变得越来越多，下面总结了一些Collection的常用方法，方便以后使用。
简介Illuminate\Support\Collection类提供了一个流畅、便利的封装来操控数组数据。
创建集合如上所述，collect() 辅助函数会利用传入的">
<meta property="og:image" content="http://yoursite.com/uploads/users-collections.png">
<meta property="og:updated_time" content="2017-06-07T00:43:57.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Laravel Collection常用方法">
<meta name="twitter:description" content="自从Laravel 5.3开始，关于Eloquent ORM操作的返回值变成了Collection，不再是数组，意味着我们对Collection的操作将会变得越来越多，下面总结了一些Collection的常用方法，方便以后使用。
简介Illuminate\Support\Collection类提供了一个流畅、便利的封装来操控数组数据。
创建集合如上所述，collect() 辅助函数会利用传入的">
<meta name="twitter:image" content="http://yoursite.com/uploads/users-collections.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/06/05/Laravel-Collection常用方法/"/>





  <title> Laravel Collection常用方法 | EasyBlog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?07a6d49ae67fe991e0ce3c2f3955e0f9";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">EasyBlog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/06/05/Laravel-Collection常用方法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                Laravel Collection常用方法
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-05T20:17:15+08:00">
                2017-06-05
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/06/05/Laravel-Collection常用方法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/06/05/Laravel-Collection常用方法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>  自从<code>Laravel 5.3</code>开始，关于<code>Eloquent ORM</code>操作的返回值变成了<code>Collection</code>，不再是数组，意味着我们对<code>Collection</code>的操作将会变得越来越多，下面总结了一些<code>Collection</code>的常用方法，方便以后使用。</p>
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p><code>Illuminate\Support\Collection</code>类提供了一个流畅、便利的封装来操控数组数据。</p>
<h2 id="创建集合"><a href="#创建集合" class="headerlink" title="创建集合"></a>创建集合</h2><p>如上所述，<code>collect()</code> 辅助函数会利用传入的数组生成一个新的 <code>Illuminate\Support\Collection</code> 实例。所以要创建一个集合就这么简单：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$newCollection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div></pre></td></tr></table></figure>
<p><code>Collection</code> 类支持链式调用，一般来说，每一个 <code>Collection</code> 方法会返回一个全新的 <code>Collection</code> 实例，你可以放心地进行链接调用。</p>
<h2 id="Eloquent-ORM-Collection"><a href="#Eloquent-ORM-Collection" class="headerlink" title="Eloquent ORM Collection"></a>Eloquent ORM Collection</h2><blockquote>
<p>默认 Eloquent 模型的查询结果总是以 <code>Collection</code> 实例返回。</p>
</blockquote>
<p>进入tinker,我们可以用factory方法在<code>users table</code>中生成10条数据。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">factory(App\User::class,<span class="number">10</span>)-&gt;create();</div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Get a list of users from the users table</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getUsers</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">    $users = User::all();</div><div class="line">    dd($users);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<a id="more"></a>
<p>下面显示<code>getUser()</code>函数返回一个包含所有users的<code>collection</code>集合<br><img src="/uploads/users-collections.png" alt="users-collections"></p>
<p>如果只想得到一个用户的信息可以使用<code>first()</code>方法</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Get the name of the first user</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">firstUser</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">    $user = User::first();</div><div class="line">    dd($user-&gt;name);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="查找数据"><a href="#查找数据" class="headerlink" title="查找数据"></a>查找数据</h2><h4 id="contains"><a href="#contains" class="headerlink" title="contains"></a>contains</h4><p>判断集合是否含有指定项目,返回值为布尔值:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="string">'name'</span> =&gt; <span class="string">'Desk'</span>, <span class="string">'price'</span> =&gt; <span class="number">100</span>]);</div><div class="line"></div><div class="line">$collection-&gt;contains(<span class="string">'Desk'</span>);</div><div class="line"></div><div class="line"><span class="comment">// true</span></div><div class="line"></div><div class="line">$collection-&gt;contains(<span class="string">'New York'</span>);</div><div class="line"></div><div class="line"><span class="comment">// false</span></div></pre></td></tr></table></figure>
<p>可以将键值对传入<code>contains</code>方法，用来判断该组合是否存在于集合内:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$collection = collect([</div><div class="line">    [<span class="string">'product'</span> =&gt; <span class="string">'Desk'</span>, <span class="string">'price'</span> =&gt; <span class="number">200</span>],</div><div class="line">    [<span class="string">'product'</span> =&gt; <span class="string">'Chair'</span>, <span class="string">'price'</span> =&gt; <span class="number">100</span>],</div><div class="line">]);</div><div class="line"></div><div class="line">$collection-&gt;contains(<span class="string">'product'</span>, <span class="string">'Bookcase'</span>);</div><div class="line"></div><div class="line"><span class="comment">// false</span></div></pre></td></tr></table></figure>
<p>还可以传入一个回调函数到<code>contains</code>方法内运行自己的判断语句:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div><div class="line"></div><div class="line">$collection-&gt;contains(<span class="function"><span class="keyword">function</span> <span class="params">($value, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $value &gt; <span class="number">5</span>;</div><div class="line">&#125;);</div><div class="line"></div><div class="line"><span class="comment">// false</span></div></pre></td></tr></table></figure>
<h4 id="where"><a href="#where" class="headerlink" title="where"></a>where</h4><p>可以使用<code>where</code>方法通过给定的键值对来搜索集合:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Use the where method to find data that matches a given</div><div class="line"> * criteria.</div><div class="line"> *</div><div class="line"> * Chain the methods for fine-tuned criteria</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">where</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">    $users = User::all();</div><div class="line">    $user = $users-&gt;where(<span class="string">'id'</span>, <span class="number">2</span>);</div><div class="line">    <span class="comment">//Collection of user with an ID of 2</span></div><div class="line"></div><div class="line">    $user = $users-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>)</div><div class="line">                  -&gt;where(<span class="string">'age'</span>, <span class="string">'51'</span>)</div><div class="line">                  -&gt;where(<span class="string">'name'</span>, <span class="string">'Chasity Tillman'</span>);</div><div class="line"></div><div class="line">    <span class="comment">//collection of user with an id of 1, age 51</span></div><div class="line">    <span class="comment">//and named Chasity Tillman</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>还有一些类似where这样的方法，你可以去<a href="http://d.laravel-china.org/docs/5.4/collections" target="_blank" rel="external">Laravel文档</a>中仔细查看。</p>
<p>这个有三个方法值得你去看看：</p>
<ul>
<li>whereIn() - 基于参数中的键值数组进行过滤</li>
<li>search() - 在集合内搜索指定的数值并返回找到的键。假如找不到项目，则返回 <code>false</code></li>
<li>has() - 检查集合中是否含有指定的「键」,返回布尔值</li>
</ul>
<h2 id="过滤数据"><a href="#过滤数据" class="headerlink" title="过滤数据"></a>过滤数据</h2><p>在集合中的过滤是使用<code>filter()</code>方法,通过回调函数筛选集合，只留下通过判断测试的项目:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</div><div class="line"></div><div class="line">$filtered = $collection-&gt;filter(<span class="function"><span class="keyword">function</span> <span class="params">($value, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $value &gt; <span class="number">2</span>;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">$filtered-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [3, 4]</span></div></pre></td></tr></table></figure>
<p>如果没有提供回调函数，集合中所有返回 <code>false</code> 的元素都会被移除：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="keyword">null</span>, <span class="keyword">false</span>, <span class="string">''</span>, <span class="number">0</span>, []]);</div><div class="line"></div><div class="line">$collection-&gt;filter()-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [1, 2, 3]</span></div></pre></td></tr></table></figure>
<h2 id="数据排序"><a href="#数据排序" class="headerlink" title="数据排序"></a>数据排序</h2><p>关于集合的排序很简单，集合提供了两种方法:</p>
<ul>
<li>sortBy() - 根据一定标准进行排升序</li>
<li>sortByDesc() - 根据一定标准排降序</li>
</ul>
<p><code>sort()</code>方法可以使用给定键值或回调函数进行排序</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * The sort methods takes a key or callback function parameter</div><div class="line"> * which is used to sort a collection.</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">sortData</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">    $users  = User::all();</div><div class="line"></div><div class="line">    $youngestToOldest = $users-&gt;sortBy(<span class="string">'age'</span>);</div><div class="line">    $youngestToOldest-&gt;all();</div><div class="line">    <span class="comment">//list of all users from youngest to oldest</span></div><div class="line"></div><div class="line">    $movies = collect([</div><div class="line">        [</div><div class="line">            <span class="string">'name'</span> =&gt; <span class="string">'Back To The Future'</span>,</div><div class="line">            <span class="string">'releases'</span> =&gt; [<span class="number">1985</span>, <span class="number">1989</span>, <span class="number">1990</span>]</div><div class="line">        ],</div><div class="line">        [</div><div class="line">            <span class="string">'name'</span> =&gt; <span class="string">'Fast and Furious'</span>,</div><div class="line">            <span class="string">'releases'</span> =&gt; [<span class="number">2001</span>, <span class="number">2003</span>, <span class="number">2006</span>, <span class="number">2009</span>, <span class="number">2011</span>, <span class="number">2013</span>, <span class="number">2015</span>, <span class="number">2017</span>]</div><div class="line">        ],</div><div class="line">        [</div><div class="line">            <span class="string">'name'</span> =&gt; <span class="string">'Speed'</span>,</div><div class="line">            <span class="string">'releases'</span> =&gt; [<span class="number">1994</span>]</div><div class="line">        ]</div><div class="line">    ]);</div><div class="line"></div><div class="line">    $mostReleases = $movies-&gt;sortByDesc(<span class="function"><span class="keyword">function</span> <span class="params">($movie, $key)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> count($movie[<span class="string">'releases'</span>]);</div><div class="line">    &#125;);</div><div class="line"></div><div class="line">    $mostReleases-&gt;toArray();</div><div class="line">    <span class="comment">//list of movies in descending order of most releases.</span></div><div class="line"></div><div class="line">    dd($mostReleases-&gt;values()-&gt;toArray());</div><div class="line">    <span class="comment">/*</span></div><div class="line">        list of movies in descending order of most releases</div><div class="line">        but with the key values reset</div><div class="line">    */</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><code>values()</code>方法来把键重置为连续数字的键,<code>toArray()</code>方法将集合转换成数组。</p>
<h2 id="数据分组"><a href="#数据分组" class="headerlink" title="数据分组"></a>数据分组</h2><h4 id="GroupBy"><a href="#GroupBy" class="headerlink" title="GroupBy"></a>GroupBy</h4><p>根据指定的「键」为集合内的项目分组：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">$collection = collect([</div><div class="line">    [<span class="string">'account_id'</span> =&gt; <span class="string">'account-x10'</span>, <span class="string">'product'</span> =&gt; <span class="string">'Chair'</span>],</div><div class="line">    [<span class="string">'account_id'</span> =&gt; <span class="string">'account-x10'</span>, <span class="string">'product'</span> =&gt; <span class="string">'Bookcase'</span>],</div><div class="line">    [<span class="string">'account_id'</span> =&gt; <span class="string">'account-x11'</span>, <span class="string">'product'</span> =&gt; <span class="string">'Desk'</span>],</div><div class="line">]);</div><div class="line"></div><div class="line">$grouped = $collection-&gt;groupBy(<span class="string">'account_id'</span>);</div><div class="line"></div><div class="line">$grouped-&gt;toArray();</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line">    [</div><div class="line">        'account-x10' =&gt; [</div><div class="line">            ['account_id' =&gt; 'account-x10', 'product' =&gt; 'Chair'],</div><div class="line">            ['account_id' =&gt; 'account-x10', 'product' =&gt; 'Bookcase'],</div><div class="line">        ],</div><div class="line">        'account-x11' =&gt; [</div><div class="line">            ['account_id' =&gt; 'account-x11', 'product' =&gt; 'Desk'],</div><div class="line">        ],</div><div class="line">    ]</div><div class="line">*/</div></pre></td></tr></table></figure>
<p>除了传入字符串的「键」之外，你也可以传入回调函数。该函数应该返回你希望用来分组的键的值。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">$grouped = $collection-&gt;groupBy(<span class="function"><span class="keyword">function</span> <span class="params">($item, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> substr($item[<span class="string">'account_id'</span>], <span class="number">-3</span>);</div><div class="line">&#125;);</div><div class="line"></div><div class="line">$grouped-&gt;toArray();</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line">    [</div><div class="line">        'x10' =&gt; [</div><div class="line">            ['account_id' =&gt; 'account-x10', 'product' =&gt; 'Chair'],</div><div class="line">            ['account_id' =&gt; 'account-x10', 'product' =&gt; 'Bookcase'],</div><div class="line">        ],</div><div class="line">        'x11' =&gt; [</div><div class="line">            ['account_id' =&gt; 'account-x11', 'product' =&gt; 'Desk'],</div><div class="line">        ],</div><div class="line">    ]</div><div class="line">*/</div></pre></td></tr></table></figure>
<h2 id="获取数据的子集"><a href="#获取数据的子集" class="headerlink" title="获取数据的子集"></a>获取数据的子集</h2><p>给定一组数据，肯能你只想要其中的一部分，那么下面几种方法会帮助你。</p>
<h4 id="take"><a href="#take" class="headerlink" title="take"></a>take</h4><p>返回有着指定数量项目的集合：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div><div class="line"></div><div class="line">$chunk = $collection-&gt;take(<span class="number">3</span>);</div><div class="line"></div><div class="line">$chunk-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [0, 1, 2]</span></div></pre></td></tr></table></figure>
<p>你也可以传入负整数以获取从集合后面来算指定数量的项目：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div><div class="line"></div><div class="line">$chunk = $collection-&gt;take(<span class="number">-2</span>);</div><div class="line"></div><div class="line">$chunk-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [4, 5]</span></div></pre></td></tr></table></figure>
<h4 id="chunk"><a href="#chunk" class="headerlink" title="chunk"></a>chunk</h4><p>将集合拆成多个指定大小的较小集合：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]);</div><div class="line"></div><div class="line">$chunks = $collection-&gt;chunk(<span class="number">4</span>);</div><div class="line"></div><div class="line">$chunks-&gt;toArray();</div><div class="line"></div><div class="line"><span class="comment">// [[1, 2, 3, 4], [5, 6, 7]]</span></div></pre></td></tr></table></figure>
<p>这个方法在适用于网格系统如 Bootstrap 的 视图 。想像你有一个 Eloquent 模型的集合要显示在一个网格内：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">@foreach ($products-&gt;chunk(3) as $chunk)</div><div class="line">    &lt;div class="row"&gt;</div><div class="line">        @foreach ($chunk as $product)</div><div class="line">            &lt;div class="col-xs-4"&gt;&#123;&#123; $product-&gt;name &#125;&#125;&lt;/div&gt;</div><div class="line">        @endforeach</div><div class="line">    &lt;/div&gt;</div><div class="line">@endforeach</div></pre></td></tr></table></figure>
<h2 id="遍历数据"><a href="#遍历数据" class="headerlink" title="遍历数据"></a>遍历数据</h2><h4 id="map"><a href="#map" class="headerlink" title="map"></a>map</h4><p>遍历整个集合并将每一个数值传入回调函数。回调函数可以任意修改并返回项目，形成修改过的项目组成的新集合：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div><div class="line"></div><div class="line">$multiplied = $collection-&gt;map(<span class="function"><span class="keyword">function</span> <span class="params">($item, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $item * <span class="number">2</span>;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">$multiplied-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [2, 4, 6, 8, 10]</span></div></pre></td></tr></table></figure>
<h4 id="transform"><a href="#transform" class="headerlink" title="transform"></a>transform</h4><p>遍历集合并对集合内每一个项目调用指定的回调函数。集合的项目将会被回调函数返回的数值取代掉：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div><div class="line"></div><div class="line">$collection-&gt;transform(<span class="function"><span class="keyword">function</span> <span class="params">($item, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $item * <span class="number">2</span>;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">$collection-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [2, 4, 6, 8, 10]</span></div></pre></td></tr></table></figure>
<blockquote>
<p>与大多数其它集合的方法不同，<code>transform</code> 会修改集合本身。如果你希望创建新集合，就改用 <code>map</code> 方法。</p>
</blockquote>
<h4 id="reduce"><a href="#reduce" class="headerlink" title="reduce"></a>reduce</h4><p><code>reduce</code> 方法将集合缩减到单个数值，该方法会将每次迭代的结果传入到下一次迭代:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</div><div class="line"></div><div class="line">$total = $collection-&gt;reduce(<span class="function"><span class="keyword">function</span> <span class="params">($carry, $item)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $carry + $item;</div><div class="line">&#125;);</div><div class="line"></div><div class="line"><span class="comment">// 6</span></div></pre></td></tr></table></figure>
<p>第一次迭代时 $carry 的数值为 null；然而你也可以传入第二个参数进 reduce 以指定它的初始值：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">$collection-&gt;reduce(<span class="function"><span class="keyword">function</span> <span class="params">($carry, $item)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $carry + $item;</div><div class="line">&#125;, <span class="number">4</span>);</div><div class="line"></div><div class="line"><span class="comment">// 10</span></div></pre></td></tr></table></figure>
<h4 id="each"><a href="#each" class="headerlink" title="each"></a>each</h4><p>遍历集合中的项目，并将之传入回调函数：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$collection = $collection-&gt;each(<span class="function"><span class="keyword">function</span> <span class="params">($item, $key)</span> </span>&#123;</div><div class="line">    <span class="comment">//</span></div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>回调函数中返回 <code>false</code> 以中断循环：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$collection = $collection-&gt;each(<span class="function"><span class="keyword">function</span> <span class="params">($item, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (<span class="comment">/* some condition */</span>) &#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">    &#125;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<h4 id="every"><a href="#every" class="headerlink" title="every"></a>every</h4><p>判断集合中每一个元素是否都符合指定条件：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])-&gt;every(<span class="function"><span class="keyword">function</span> <span class="params">($value, $key)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> $value &gt; <span class="number">2</span>;</div><div class="line">&#125;);</div><div class="line"></div><div class="line"><span class="comment">// false</span></div></pre></td></tr></table></figure>
<h2 id="集合之间的操作"><a href="#集合之间的操作" class="headerlink" title="集合之间的操作"></a>集合之间的操作</h2><h4 id="union"><a href="#union" class="headerlink" title="union"></a>union</h4><p>将给定的数组合并到集合中，如果数组中含有与集合一样的「键」，集合的键值会被保留：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="number">1</span> =&gt; [<span class="string">'a'</span>], <span class="number">2</span> =&gt; [<span class="string">'b'</span>]]);</div><div class="line"></div><div class="line">$union = $collection-&gt;union([<span class="number">3</span> =&gt; [<span class="string">'c'</span>], <span class="number">1</span> =&gt; [<span class="string">'b'</span>]]);</div><div class="line"></div><div class="line">$union-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [1 =&gt; ['a'], 2 =&gt; ['b'], 3 =&gt; ['c']]</span></div></pre></td></tr></table></figure>
<h4 id="intersect"><a href="#intersect" class="headerlink" title="intersect"></a>intersect</h4><p>移除任何指定 数组 或集合内所没有的数值。最终集合保存着原集合的键：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$collection = collect([<span class="string">'Desk'</span>, <span class="string">'Sofa'</span>, <span class="string">'Chair'</span>]);</div><div class="line"></div><div class="line">$intersect = $collection-&gt;intersect([<span class="string">'Desk'</span>, <span class="string">'Chair'</span>, <span class="string">'Bookcase'</span>]);</div><div class="line"></div><div class="line">$intersect-&gt;all();</div><div class="line"></div><div class="line"><span class="comment">// [0 =&gt; 'Desk', 2 =&gt; 'Chair']</span></div></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>上述包含了大部分方法，不过还是有很多需要学习的。最好还是去看看<a href="http://d.laravel-china.org/docs/5.4/collections" target="_blank" rel="external">Laravel文档</a>。</p>
<p>本文内容来自于：</p>
<ul>
<li><a href="http://d.laravel-china.org/docs/5.4/collections" target="_blank" rel="external">Laravel文档</a></li>
<li><a href="Laravel Collections: PHP Arrays On Steroids">Laravel Collections: PHP Arrays On Steroids</a></li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Laravel/" rel="tag"># Laravel</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/23/Laravel如何连接多个数据库？/" rel="next" title="Laravel如何连接多个数据库？">
                <i class="fa fa-chevron-left"></i> Laravel如何连接多个数据库？
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://gasbylei.github.io/uploads/avatar.png"
               alt="Gasbylei" />
          <p class="site-author-name" itemprop="name">Gasbylei</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/Gasbylei" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建集合"><span class="nav-number">2.</span> <span class="nav-text">创建集合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Eloquent-ORM-Collection"><span class="nav-number">3.</span> <span class="nav-text">Eloquent ORM Collection</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查找数据"><span class="nav-number">4.</span> <span class="nav-text">查找数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#contains"><span class="nav-number">4.0.1.</span> <span class="nav-text">contains</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#where"><span class="nav-number">4.0.2.</span> <span class="nav-text">where</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#过滤数据"><span class="nav-number">5.</span> <span class="nav-text">过滤数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据排序"><span class="nav-number">6.</span> <span class="nav-text">数据排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据分组"><span class="nav-number">7.</span> <span class="nav-text">数据分组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#GroupBy"><span class="nav-number">7.0.1.</span> <span class="nav-text">GroupBy</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取数据的子集"><span class="nav-number">8.</span> <span class="nav-text">获取数据的子集</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#take"><span class="nav-number">8.0.1.</span> <span class="nav-text">take</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#chunk"><span class="nav-number">8.0.2.</span> <span class="nav-text">chunk</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#遍历数据"><span class="nav-number">9.</span> <span class="nav-text">遍历数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#map"><span class="nav-number">9.0.1.</span> <span class="nav-text">map</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#transform"><span class="nav-number">9.0.2.</span> <span class="nav-text">transform</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#reduce"><span class="nav-number">9.0.3.</span> <span class="nav-text">reduce</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#each"><span class="nav-number">9.0.4.</span> <span class="nav-text">each</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#every"><span class="nav-number">9.0.5.</span> <span class="nav-text">every</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#集合之间的操作"><span class="nav-number">10.</span> <span class="nav-text">集合之间的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#union"><span class="nav-number">10.0.1.</span> <span class="nav-text">union</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#intersect"><span class="nav-number">10.0.2.</span> <span class="nav-text">intersect</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">11.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Gasbylei</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  

    <script type="text/javascript">
      var disqus_shortname = 'gatsbylei';
      var disqus_identifier = '2017/06/05/Laravel-Collection常用方法/';

      var disqus_title = "Laravel Collection常用方法";


      function run_disqus_script(disqus_script) {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
      }

      run_disqus_script('count.js');

      
        var disqus_config = function () {
            this.page.url = disqus_url;
            this.page.identifier = disqus_identifier;
            this.page.title = disqus_title;
        };
        run_disqus_script('embed.js');
      

    </script>
  










  
  

  

  

  

  


  

</body>
</html>
